var request = undefined;
var equipmentData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	getRealTimeStatus();
	setTimeout(hideLoading, 500);
})

function getRealTimeStatus() {
	var ajax = new createAjax();
	ajax.type = "post";
	ajax.dataType = "json";
	ajax.url = "realTimeStatus";
	ajax.timeout = 30000;
	ajax.success = function(data, textStatus) {
		var length = data.length;
		equipmentData = data;
		//清空数据
		$('#msg').html('');
		$('#parkingspace').html('');
		$('#chargingpile').html('');
		$('#car').html('');
		$('#1msg').html('');
		var mainContentDiv = $('#msg');
		var parkingSpace = $('#parkingspace');
		var chargingpile = $('#chargingpile');
		var car = $('#car');
		for (var i=0;i<length;i++) {
			var div = document.createElement("span");
			div.setAttribute("id", "msg" + (i + 1));
			var span1 = document.createElement("span");
			span1.setAttribute("class","span1");
			span1.innerHTML = data[i] && data[i].EquipmentName;
			var span2 = document.createElement("span");
			span2.innerHTML = data[i] && ("通信状态：" + data[i].communicationState);
			//充电机
			var chargingPile = document.createElement("span");
			//车辆
			var Car = document.createElement("span");
			chargingPile.setAttribute("id", "chargingPile" + (i+1));
			//消息框上下层判断
			if(data[i].communicationState == "正常"){
				if(i % 2 == 0){
					if(data[i].deviceState == "充电中"){
						div.setAttribute("class", "charging2");
						chargingPile.setAttribute("class", "chargingPile02");
					}else if(data[i].deviceState == "空闲"){//空闲
						div.setAttribute("class", "leisure2");
						chargingPile.setAttribute("class", "leisure2");
					}else{//故障
						div.setAttribute("class", "malfunction2");
						chargingPile.setAttribute("class", "chargingPile04");
					}
				}else{
					 if(data[i].deviceState == "充电中"){
						div.setAttribute("class", "charging1");
						chargingPile.setAttribute("class", "chargingPile02");
					}else if(data[i].deviceState == "空闲"){
						div.setAttribute("class", "leisure1");
						chargingPile.setAttribute("class", "leisure1");
					}else{//故障
						div.setAttribute("class", "malfunction1");
						chargingPile.setAttribute("class", "chargingPile04");
					}
				}
			} else if(data[i].communicationState == "断开"){
				if(i%2 == 0){
					div.setAttribute("class", "off2");
				}else{
					div.setAttribute("class", "off1");
				}
				chargingPile.setAttribute("class", "chargingPile03");
			}
			var img1 = document.createElement("img");
			var img2 = document.createElement("img");
			span2.appendChild(img1);
			var span3 = document.createElement("span");
			span3.innerHTML = "车辆:" + data[i].ChargingGun1;
			var span4 = document.createElement("span");
			//判断枪连接状态
			if(data[i].ChargingGun1 == "已连接"){
				Car.setAttribute("id", "car" + (i+1));
				Car.setAttribute("class", "car");
			}
			chargingPile.setAttribute("onclick", "getDetatil("+i+")");
			span4.innerHTML = "工作状态:" + data[i].deviceState + " ";
			span4.appendChild(img2);
			div.appendChild(span1);
			var br = document.createElement("br");
			div.appendChild(br);
			div.appendChild(span2);
			var br = document.createElement("br");
			div.appendChild(br);
			div.appendChild(span3);
			div.appendChild(span3);
			var br = document.createElement("br");
			div.appendChild(br);
			div.appendChild(span4);
			mainContentDiv.append(div);
			//停车位
			var parkingspace= document.createElement("span");
			parkingspace.setAttribute("id", "parkingSpace" + (i+1));
			parkingspace.setAttribute("class", "parkingSpace");
			parkingSpace.append(parkingspace);
			//添加充电桩
			chargingpile.append(chargingPile);
			//添加公交车
			car.append(Car);
		}
		getRealTimeStatus();
	};
	ajax.error = function(XMLHttpRequest, textStatus, errorThrown) {
		setTimeout(hideLoading, 500);
		if (textStatus == "timeout") {
			getRealTimeStatus();
		} else if (textStatus != "abort") {
			getRealTimeStatus();
		}
	};
	if (request) {
		request.abort();
	}
	request = startAjax(ajax);
	ajax = null;
}
function getDetatil(i){
	window.parent.document.getElementById("selectEquipment").value = equipmentData[i].DevCode.equipmentName;
	window.location.href = "chargingEquipment/equipmentStatus";
}
